/*
	Copyright(c) 2010-11 Bastien Gysler <basgys@gmail.com> / David Dieulivol <david.dieulivol@gmail.com>	
	Your reuse is governed by the Apache License 2.0
	
	Website : http://code.google.com/p/put-dot-io-library/
*/
package
{
	import flash.display.Sprite;
	
	import org.osmf.proxies.ProxyElement;
	
	import proxies.Proxy;
	import proxies.classes.activetransfers.ActiveTransfersProxy;
	import proxies.classes.activetransfers.IActiveTransfersProxy;
	import proxies.classes.dashboardmessages.DashboardMessagesProxy;
	import proxies.classes.dashboardmessages.IDashboardMessagesProxy;
	import proxies.classes.files.FilesProxy;
	import proxies.classes.files.IFilesProxy;
	import proxies.classes.subscription.ISubscriptionProxy;
	import proxies.classes.subscription.SubscriptionProxy;
	import proxies.classes.url.IURLProxy;
	import proxies.classes.url.URLProxy;
	import proxies.classes.user.IUserProxy;
	import proxies.classes.user.UserProxy;
	import proxies.events.IProxyEventsDispatcher;
	import proxies.events.ProxyEvent;
	import proxies.events.ProxyEventsDispatcher;
	
	import tools.PutIoLibSettings;
	
	/**
	 * Main class of this library.
	 * <br />
	 * This class gathers all features offered by Put.io and avoids the instantiation of multiple objects.
	 * <br />
	 * In almost all cases, it is unnecessary to instantiate classes of the group "classes" except events
	 */
	public class LibraryFacade implements IActiveTransfersProxy, IDashboardMessagesProxy, IFilesProxy, ISubscriptionProxy, IURLProxy, IUserProxy, IProxyEventsDispatcher
	{		
		private static var instance:LibraryFacade;
		private static var allowInstance:Boolean = false;
		
		private var activeTransfers:ActiveTransfersProxy = new ActiveTransfersProxy();
		private var dashboardMessages:DashboardMessagesProxy = new DashboardMessagesProxy();
		private var files:FilesProxy = new FilesProxy();
		private var subscription:SubscriptionProxy = new SubscriptionProxy();
		private var url:URLProxy = new URLProxy();
		private var user:UserProxy = new UserProxy();
		
		private var proxyEventDispatcher:ProxyEventsDispatcher = ProxyEventsDispatcher.getInstance();
		
		/**
		 * This class implement the Singleton pattern so direct instance is not allowed.
		 * <br />
		 * To get an instance, use getInstance() method
		 * 
		 * @see getInstance
		 */		
		public function LibraryFacade() {
			if(!allowInstance)
				throw new Error('Multiple instance not allowed in LibraryFacade class !');			
		}		
		
		/**
		 * Get the unique instance of this class
		 * 
		 * @return the unique instance
		 */		
		public static function getInstance():LibraryFacade {
			if(instance == null) {
				allowInstance = true;
				instance = new LibraryFacade();
				allowInstance = false;
			}
			
			return instance;
		}
		
		//---------------------------------USER SPACE---------------------------------------//
		
		
		// -------------------- ActiveTransfers ---------------------//
		public function listActiveTransfers():void
		{
			activeTransfers.listActiveTransfers();
		}
		
		public function addActiveTransfer(url:Array):void
		{
			activeTransfers.addActiveTransfer(url);
		}
		
		public function cancelActiveTransfer(id:String):void
		{
			activeTransfers.cancelActiveTransfer(id);
		}
		
		
		// -------------------- DashboardMessages ---------------------//
		public function listDashboardMessages():void
		{
			dashboardMessages.listDashboardMessages();
		}

		public function deleteDashboardMessage(id:String):void
		{
			dashboardMessages.deleteDashboardMessage(id);
		}		
		
		// -------------------- FilesProxy ---------------------
		
		public function listFiles(id:int, parent_id:int, type:String, orderby:String, offset:int=0, limit:int=20):void
		{
			files.listFiles(id, parent_id, type, orderby, offset, limit);
		}		
		
		public function createDirFiles(name:String, parent_id:int):void
		{
			files.createDirFiles(name, parent_id);
		}
		
		public function infoFiles(id:int):void
		{
			files.infoFiles(id);
		}
		
		public function renameFile(id:int, name:String):void
		{
			files.renameFile(id, name);
		}
		
		public function moveFile(id:int, parentId:int):void
		{
			files.moveFile(id, parentId);
		}
		
		public function deleteFile(id:int):void
		{
			files.deleteFile(id);
		}
		
		public function searchFile(query:String):void
		{
			files.searchFile(query);
		}
		
		public function dirmapFile():void
		{
			files.dirmapFile();
		}		

		//----------------------SubscriptionProxy---------------------//
		
		public function listSubscriptions():void
		{
			subscription.listSubscriptions();
		}

		public function createSubscription(title:String, url:String, doFilters:String, dontFilters:String, parentFolderId:String, paused:Boolean):void
		{
			subscription.createSubscription(title, url, doFilters, dontFilters, parentFolderId, paused);
		}
		
		public function editSubscription(id:int, title:String, url:String, doFilters:String, dontFilters:String, parentFolderId:String, paused:Boolean):void
		{
			subscription.editSubscription(id, title, url, doFilters, dontFilters, parentFolderId, paused);
		}
		
		public function deleteSubscription(id:int):void
		{
			subscription.deleteSubscription(id);
		}
		
		public function pauseSubscription(id:int):void
		{
			subscription.pauseSubscription(id);
		}
		
		public function infoSubscription(id:int):void
		{
			subscription.infoSubscription(id);
		}
		
		// -------------------- URLProxy  ---------------------
		
		public function analyze(urls:String):void
		{
			url.analyze(urls);
		}
		
		public function extracturls(txt:String):void
		{
			url.extracturls(txt);
		}
		
		// -------------------- UserProxy ---------------------
		public function userInfo():void
		{
			user.userInfo();
		}
		
		public function userFriends():void
		{
			user.userFriends();
		}	
		
		// -------------------- Others ---------------------//
		
		public function addEventListener(eventType:String, callbackFunction:Function):void
		{
			proxyEventDispatcher.addEventListener(eventType, callbackFunction);
		}
		
		public function removeEventListener(eventType:String, callbackFunction:Function):void
		{
			proxyEventDispatcher.removeEventListener(eventType, callbackFunction);
		}
		
		public function dispatchEvent(event:ProxyEvent):void
		{
			proxyEventDispatcher.dispatchEvent(event);
		}	
		
		/**
		 * Get the API Secret uses for communications with Put.io API
		 * 
		 * @return the API Secret
		 */		
		public function get apiKey():String
		{
			return PutIoLibSettings.getInstance().apiKey;
		}
		
		/**
		 * Set the API Secret to use for communications with Put.io API
		 * 
		 * @param the API Secret
		 */			
		public function set apiKey(_apiKey:String):void
		{
			PutIoLibSettings.getInstance().apiKey = _apiKey;
		}
		
		/**
		 * Get the API Key uses for communications with Put.io API
		 * 
		 * @return the API Key
		 */				
		public function get apiSecret():String
		{
			return PutIoLibSettings.getInstance().apiSecret;
		}
		
		/**
		 * Set the API Key to use for communications with Put.io API
		 * 
		 * @param the API Key
		 */			
		public function set apiSecret(_apiSecret:String):void
		{
			PutIoLibSettings.getInstance().apiSecret = _apiSecret;
		}
	}
}